// eee.Sheffield.PZ.Imaging
//
// Copyright ?Ping Zou, 2006
// sg71.cherub@gmail.com
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace eee.Sheffield.PZ.Imaging.ImageIO
{
    public abstract class ImageIO : IImageIO
    {
        #region Fields
        protected string _fileName = null;
        protected Bitmap _image = null;
        #endregion

        #region Properties
        public string FileName { get { return _fileName; } set { _fileName = value; } }
        public Bitmap Image { get { return _image; } set { _image = value; } }
        #endregion

        #region Constructors
        public ImageIO(string fileName)
        {
            _fileName = fileName;
        }

        public ImageIO(string fileName, Bitmap image)
        {
            _fileName = fileName;
            _image = image;
        }
        #endregion

        #region interface methods implementation
        public abstract void LoadImage();
        public abstract void SaveImage();

        ///// <summary>
        ///// load image
        ///// </summary>
        //public void LoadImage()
        //{
        //    // get file extenstion
        //    string extension = Path.GetExtension(_fileName);
        //    extension = extension.ToLower();
        //    try
        //    {
        //        switch (extension)
        //        {
        //            case ".pgm":
        //                PGMImageIO pgmImageIO = new PGMImageIO(_fileName);
        //                pgmImageIO.LoadImage();
        //                _image = (Bitmap)pgmImageIO.Image.Clone();
        //                break;
        //            case ".ppm":
        //                PPMImageIO ppmImageIO = new PPMImageIO(_fileName);
        //                ppmImageIO.LoadImage();
        //                _image = (Bitmap)ppmImageIO.Image.Clone();
        //                break;
        //            case ".dcm":
        //                throw new ApplicationException("ImageIO::LoadImage(), DICOM image is not supported yet");
        //            case ".bmp":
        //                _image = (Bitmap)Bitmap.FromFile(_fileName);
        //                break;
        //            case ".jpg":
        //                _image = (Bitmap)Bitmap.FromFile(_fileName);
        //                break;
        //            case ".png":
        //                _image = (Bitmap)Bitmap.FromFile(_fileName);
        //                break;
        //            case ".tif":
        //                _image = (Bitmap)Bitmap.FromFile(_fileName);
        //                break;
        //            default:
        //                throw new ApplicationException("ImageIO::LoadImage(), Unkown image type");
        //        }

        //        // format image
        //        if (_image.PixelFormat != PixelFormat.Format24bppRgb)
        //            eee.Sheffield.PZ.Imaging.Image.StandardizeImageTo24bppRGB(ref _image);
        //    }
        //    catch (ApplicationException e)
        //    {
        //        throw new ApplicationException("ImageIO::LoadImage(), Failed loading image" + e.Message);
        //    }
        //}

        ///// <summary>
        ///// save image
        ///// </summary>
        //public void SaveImage()
        //{
        //    // get file extenstion
        //    string extension = Path.GetExtension(_fileName);
        //    extension = extension.ToLower();
        //    try
        //    {
        //        if (extension.Equals(".pgm"))
        //        {
        //            PGMImageIO pgmImageIO = new PGMImageIO(_fileName, _image);
        //            pgmImageIO.SaveImage();
        //        }
        //        else if (extension.Equals(".ppm"))
        //        {
        //            PPMImageIO ppmImageIO = new PPMImageIO(_fileName, _image);
        //            ppmImageIO.SaveImage();
        //        }
        //        else
        //        {
        //            ImageFormat imageFormat;
        //            switch (extension)
        //            {
        //                case ".jpg":
        //                    imageFormat = ImageFormat.Jpeg;
        //                    break;
        //                case ".bmp":
        //                    imageFormat = ImageFormat.Bmp;
        //                    break;
        //                default:
        //                    throw new ApplicationException("ImageIO:SaveFile(), Unsupported image format");
        //            }
        //            _image.Save(_fileName, imageFormat);
        //        }

        //        // format image
        //        eee.Sheffield.PZ.Imaging.Image.StandardizeImageTo24bppRGB(ref _image);
        //    }
        //    catch (ApplicationException e)
        //    {
        //        throw new ApplicationException("ImageIO::SaveImage(), Failed saving image " + e.Message);
        //    }
        //}
        #endregion
    }
}